'*******************************
' Spawns number of random files
'*******************************

Dim retCode : retCode = 1

If WScript.Arguments.Count < 2 Or WScript.Arguments.Count > 4  Then
	WScript.Echo "Usage: fspawn.vbs <path> <numfiles> [<size_min>] <size_max>"
	WScript.Quit retCode
End If

Const ForWriting = 2
Const MaxRubbishSize = 1048576

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim fileCount, nameIndex, charCount, sizeMin, sizeMax, fileSize
Dim objFile
Dim fileName

If WScript.Arguments(1) < 1 Then
	WScript.Echo "bogus number of files"
	WScript.Quit retCode
End If

If WScript.Arguments.Count = 3 Then
	sizeMin = 0
	sizeMax = Int(WScript.Arguments(2))
Else
	sizeMin = Int(WScript.Arguments(2))
	sizeMax = Int(WScript.Arguments(3))
End If

If sizeMin > sizeMax Or sizeMin < 0 Or sizeMax < 0 Then
	WScript.Echo "bad size constraints"
	WScript.Quit retCode
End If

Randomize

Dim nameSalt : nameSalt = ".0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"

For fileCount = 0 To WScript.Arguments(1) - 1
	fileName = ""
	For charCount = 0 To 5
		fileName = fileName & Mid(nameSalt, 1 + Int(Rnd * Len(nameSalt)), 1)
	Next

	Set objFile = objFSO.OpenTextFile(WScript.Arguments(0) & "\" & fileName, ForWriting, True)

	fileSize = sizeMin + Round(Rnd * (sizeMax - sizeMin))

	If fileSize > 0 Then
		For charCount = 0 To fileSize - 1
			objFile.Write Chr(Int(Rnd * 128))
		Next
	End If

	objFile.Close
	Set objFile = Nothing
Next

Set objFSO = Nothing

retCode = 0

WScript.Quit retCode
